package com.pengshun.modules.api.order.controller;


import com.fasterxml.jackson.annotation.JsonView;
import com.pengshun.config.NoRepeatSubmit;
import com.pengshun.modules.api.coal.validator.*;
import com.pengshun.modules.api.order.service.MerchantEvaluateService;
import com.pengshun.modules.api.order.service.dto.MerchantEvaluateDto;
import com.pengshun.modules.api.pay.service.WechatPayService;
import com.pengshun.modules.common.ReslutView;
import com.pengshun.modules.domain.User;
import com.pengshun.modules.api.order.service.BuyUserOrderService;
import com.pengshun.modules.api.order.service.dto.MerchantOrderDto;
import com.pengshun.modules.security.Authorization;
import com.pengshun.modules.security.CurrUserInfo;
import com.pengshun.modules.common.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequiredArgsConstructor
@Api(tags = "买家订单管理")
@RequestMapping("/api/user/order")
@ApiIgnore
public class BuyUserOrderController {

    private final BuyUserOrderService buos;
    private final MerchantEvaluateService merchantEvaluateService;
    @ApiOperation(value = "下单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchantCoalId", value = "商户煤炭id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "number", value = "数量", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "totalPrice", value = "总价", dataType = "double", required = true, paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "transportWay", value = "运输方式(1委托经销商指定运输公司，2自运,3,自己指定运输公司，4自己指定车主，5自己指定司机)", dataType = "int", required = true, paramType = "query"),

            @ApiImplicitParam(name = "userAddressId", value = "地址id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sdId", value = "区编码", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "name", value = "姓名", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "电话", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "lngX", value = "经度", dataType = "double", required = true, paramType = "query"),
            @ApiImplicitParam(name = "latY", value = "纬度", dataType = "double", required = true, paramType = "query"),
            @ApiImplicitParam(name = "type", value = "1煤款+运费、2煤款、3运费", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @PostMapping(value = "/buy", produces = "application/json; charset=utf-8")
    @Authorization
    @NoRepeatSubmit
    public BaseResult buy(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {First.class}) MerchantOrderDto para) throws Exception {
        para.setUserId(user.getId());
        para.setUserName(user.getName());
        BaseResult res = buos.buy(para);
        return res;
    }
    @ApiOperation(value = "订单地址修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sdId", value = "区编码", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "name", value = "姓名", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "电话", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "lngX", value = "经度", dataType = "double", required = true, paramType = "query"),
            @ApiImplicitParam(name = "latY", value = "纬度", dataType = "double", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @PostMapping(value = "/update-address", produces = "application/json; charset=utf-8")
    @Authorization
    public BaseResult updateAddress(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {Second.class}) MerchantOrderDto para) throws Exception {
        para.setUserId(user.getId());
        BaseResult res = buos.updateAddress(para);
        return res;
    }

    @ApiOperation(value = "买家删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @PostMapping(value = "/delete", produces = "application/json; charset=utf-8")
    @Authorization
    public BaseResult delete(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {Second.class}) MerchantOrderDto para) throws Exception {
        para.setUserId(user.getId());
        para.setUserName(user.getName());
        BaseResult res = buos.delete(para);
        return res;
    }


    @ApiOperation(value = "订单确认收货")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "商户煤炭id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @PostMapping(value = "/sure-delivery", produces = "application/json; charset=utf-8")
    @Authorization
    @NoRepeatSubmit
    public BaseResult sureDelivery(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {Fifth.class}) MerchantOrderDto para) throws Exception {
        para.setUserId(user.getId());
        BaseResult res = buos.sureDelivery(para);
        return res;
    }


    @ApiOperation(value = "订单支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "煤炭订单id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "payWay", value = "支付方式(1余额，2微信)", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "payPass", value = "支付密码", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @PostMapping(value = "/pay", produces = "application/json; charset=utf-8")
    @Authorization
    @NoRepeatSubmit
    public BaseResult pay(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {Second.class}) MerchantOrderDto para) throws Exception {
        para.setUserId(user.getId());
        BaseResult res = buos.pay(para);
        return res;
    }

    @ApiOperation(value = "用户订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1待确认、3待支付、6待收货,7待评价", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "size", value = "条数", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @GetMapping(value = "/user-list", produces = "application/json; charset=utf-8")
    @Authorization
    @JsonView(ReslutView.aView.class)
    public BaseResult userList(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {Fourth.class}) MerchantOrderDto para) throws Exception {
        para.setUserId(user.getId());
        BaseResult res = buos.selectUserOrderList(para);
        return res;
    }

    @ApiOperation(value = "订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @GetMapping(value = "/detil", produces = "application/json; charset=utf-8")
    @Authorization
    @JsonView(ReslutView.aView.class)
    public BaseResult orderDetil(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {Fifth.class}) MerchantOrderDto para) throws Exception {
        para.setUserId(user.getId());
        BaseResult res = buos.orderDetil(para);
        return res;
    }

    @ApiOperation(value = "所有省市区")
    @GetMapping(value = "/allcity", produces = "application/json; charset=utf-8")
    @JsonView(ReslutView.aView.class)
    public BaseResult allcity()  throws Exception {
        BaseResult res = buos.allcity();
        return res;
    }

    @ApiOperation(value = "新增评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "score", value = "评分", dataType = "double", required = true, paramType = "query"),
            @ApiImplicitParam(name = "content", value = "评论内容", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "merchantId", value = "商户id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "firstImage", value = "图片1", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "secondImage", value = "图片2", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "thirdImage", value = "图片3", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "fourImage", value = "图片4", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "fiveImage", value = "图片5", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @GetMapping(value = "/addComments", produces = "application/json; charset=utf-8")
    @Authorization
    @JsonView(ReslutView.aView.class)
    public BaseResult addComments(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {First.class}) MerchantEvaluateDto para) throws Exception {
        para.setUserId(user.getId());
        BaseResult res = merchantEvaluateService.addComments(para);
        return res;
    }

    @ApiOperation(value = "删除评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchantEvaluateId", value = "评论id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @GetMapping(value = "/deleteComments", produces = "application/json; charset=utf-8")
    @Authorization
    @JsonView(ReslutView.aView.class)
    public BaseResult deleteComments(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {Second.class}) MerchantEvaluateDto para) throws Exception {
        BaseResult res = merchantEvaluateService.deleteComments(para);
        return res;
    }



}
